package com.snape.controller;

import com.snape.entity.Room;
import com.snape.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

//
/**
 * Created by liuqingcan_snape on 2017/4/2.
 */
@Controller
@RequestMapping(value = "room")
public class RoomController {
    @Autowired
    RoomService roomService;

    /**
     * 获得分类信息
     * @param name
     * @return
     */
    @RequestMapping(value = "get_categoryinfo", method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> getCategoryInfoByCategoryNameAction(@RequestBody String name){
        return roomService.selectCategoryInfoByCategoryName(name);
    }

    /**
     * 获得 默认的 一页房间信息
     * @param map
     * @return
     */

//    @RequestMapping(value = "get_one_page", method = RequestMethod.POST)//,consumes = "application/json"
//    public @ResponseBody Map<String,Object>getOnePageRoomByCategoryIdAction(@RequestBody String room){
//        int roomId = Integer.parseInt(room.toString().trim());
////        Integer roomId = name.get("name");
////        int roomId = Integer.parseInt(name.toString().trim());
////        Object temp=map.get("id");
////        int roomid= (Integer)temp;
//        return roomService.selectOnePageRoomByCategoryId(roomId);
//    }

    @RequestMapping(value = "get_one_page", method = RequestMethod.POST)//,consumes = "application/json"
    public @ResponseBody Map<String,Object>getOnePageRoomByCategoryIdAction(@RequestBody Map map){
//        int roomId = Integer.parseInt(name.toString().trim());
//        Integer roomId = name.get("name");
//        int roomId = Integer.parseInt(name.totring().trim());
        Object temp=map.get("id");
        if (temp!=null){
            int roomid= Integer.parseInt(temp.toString());
            return roomService.selectOnePageRoomByCategoryId(roomid);

        }else
        {
            return roomService.selectOnePageRoomByCategoryId(0);
        }

    }

    /**
     * 获得按需的房间信息
     * @param id
     * @param pageNumber  第几页
     * @param postNumber  可选参数 -- 如果不传,默认为10.
     * @return
     */
    @RequestMapping(value = "get_room",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> getRoomByCategotyidAndPagenumber
    (@RequestBody Map map){
//        @RequestBody int id ,@RequestBody int pageNumber, int postNumber
        int id = -1;int pageNumber = -1; int postNumber = -1;

        Object tempId=map.get("id");
        Object tempPageNumber=map.get("pageNumber");
        Object tempPostNumber=map.get("postNumber");
        if (tempId!=null && tempPageNumber != null){
            id= Integer.parseInt(tempId.toString());
            pageNumber = Integer.parseInt(tempPageNumber.toString());
            if (tempPostNumber != null) {
                postNumber = Integer.parseInt(tempPostNumber.toString());
            }
        }
        return roomService.selectRoomByCategotyidAndPagenumber(id,pageNumber,postNumber);



    }


    /**
     * 获得房间的有效时间
     * @param id
     * @return
     */
    @RequestMapping(value = "get_validtime",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> getValidDateByRoomId(@RequestBody Map map){
//        return roomService.selectValidDateByRoomId(id);

        int id = -1;

        Object tempId=map.get("id");
        if (tempId!=null ){
            id= Integer.parseInt(tempId.toString());
        }
        return roomService.selectValidDateByRoomId(id);
    }




    /**
     * 获得所有的分类信息
     * @return
     */
    @RequestMapping(value = "get_all_category",method = RequestMethod.GET)
    public @ResponseBody Map<String,Object> getAllCategory(){
        return roomService.selectAllCategoryInfo();
    }

    @RequestMapping(value = "add_room",method = RequestMethod.POST)
    public @ResponseBody Map<String,Object> addRoomInfo(@RequestBody Room room){
        return roomService.insertRoomInfo(room);
    }




}